//判断复选款选择情况.
//checkname : 复选框名称（name）.
//num : 个数.
//flag : 最多或最少选 num 个 (true:最少;false最多).
function  checkboxNum(checkname,num,flag){
   	var ckname = document.getElementsByName(checkname);
   	var j=0;
   	for(var i=0;i<ckname.length;i++){
     	if(ckname[i].checked!=false){
       		j++;
    	}
   	}
   	if(flag){
	   	if(j>=num){
	    	return true;
	   	}else{
	   		return false;
	   	}
   	}else{
   		if(j<=num){
	    	return true;
	   	}else{
	   		return false;
	   	}
   	}
}


function ValidateFactory(oForm){

	this.stylePool = new Array();
	
	this.keyNumber = 1;

	this.formObj = oForm;

	this.add = function(obj){
		this.stylePool.push(obj);
	};

	this.lastPrompt = null;

	this.get = function(str){
		var eLength = this.stylePool.length;
		for(var i = 0; i < eLength; i++){
			var obj = this.stylePool[i];
			if(obj._tmpUUIDKey == str){
				return obj._tmpStyle;
			}
		}
	};

	this.getKeyNumber = function(){
		return this.keyNumber++;
	};
	
	this.setFormObj = function(obj){
		this.formObj = obj;
	};
	this.getFormObj = function(){
		return this.formObj;
	};

	this.validate = function(oForm){
		
		var formType = typeof(oForm);
		
		if(formType.toUpperCase() == "STRING"){
			oForm = document.getElementById(oForm);
		}
		if(oForm != undefined && oForm != null){
			this.setFormObj(oForm);
		}
		
		var allFlag = true;
		var inputs = this.getFormObj().getElementsByTagName("input");
		var passwords = this.getFormObj().getElementsByTagName("password");
		var textareas = this.getFormObj().getElementsByTagName("textarea");
		var selectss = this.getFormObj().getElementsByTagName("select");
		var arr = new Array();
		arr = arr.concat(this.toArray(inputs),this.toArray(passwords),this.toArray(textareas),this.toArray(selectss));
		var arrLength = arr.length;
		var elementList = new Array();
		for(var i = 0; i < arrLength; i++){
			var flag = true;
			var element = arr[i];
			if(this.isNotNull(element.getAttribute("validate"))){
				flag = this.validateMain(element);
				if(!flag){
					elementList.push(element);
				}
			}
			if(allFlag){
				allFlag = flag;
			}
		}
		
		if(!allFlag){
			if(ValidateFactory.prototype.lastPrompt != null && ValidateFactory.prototype.lastPrompt != undefined && ValidateFactory.prototype.lastPrompt instanceof  Function){
				ValidateFactory.prototype.lastPrompt(elementList);
			}
		}

		for(var i = 0; i < elementList.length; i++){
			var element = elementList[i];
			element.setAttribute("v_index", "");
		}
		return allFlag;
	};
	
	this.validateMain = function(element){
	
		var allFlag = true;
		var nodeName = element.nodeName.toUpperCase();
		var type = element.type.toUpperCase();
		var vPool = this.getValidatePool();
		var vPoolLength = vPool.length;
		for(var i = 0; i < vPoolLength; i++){
			var vObj = vPool[i];
			var vNodeName = vObj.nodeName.toUpperCase();
			if(nodeName == vNodeName){
				var vType = vObj.type.toUpperCase();
				if(type == vType){
					var validates = element.getAttribute("validate").split(",");
					var must_validate = element.getAttribute("must_validate");
					if(must_validate != null && must_validate != undefined){
						must_validate = must_validate.toUpperCase();
					}else{
						must_validate = "false";
					}
					
					if(ValidateFactory.prototype.isValidate || must_validate == "TRUE"){
						for(var j = 0; j < validates.length; j++){
							var validateStr = validates[j].toUpperCase();
							var vArr = vObj.validaterArr;
							var vArrLength = vArr.length;
							for(var k = 0; k < vArrLength; k++){
								var validater = vArr[k];
								var vName = validater.validateName.toUpperCase();
								if(validateStr == vName){
									var flag = validater.fun.call(this,element);
									if(!flag){
										var m = element.getAttribute("m"+j);
										
										if(m != undefined && m != null && ValidateFactory.prototype.trim(m) != ""){
											var v_index = element.getAttribute("v_index");
											
											if(v_index != undefined && v_index != null && ValidateFactory.prototype.trim(v_index) != ""){
												element.setAttribute("v_index", v_index + "," + j);
											}else{
												element.setAttribute("v_index", ""+j);
											}
										}
									}
	
									if(!flag && element.getAttribute("_tmpUUIDKey") == null){
										var uuidStr = this.getKeyNumber();
										element.setAttribute("_tmpUUIDKey",uuidStr);
										var tmpStyle = this.clone(element.style);
										this.add({"_tmpUUIDKey":uuidStr,"_tmpStyle":tmpStyle});
									}
									if(validater.message != undefined && validater.message != null && validater.message instanceof  Function){
										validater.message.call(this,element,flag);
									}else{
										var index = this.getMessageIndex(nodeName, type);
										if(index != -1){
											if(this.getMessagePool()[index].message != undefined && this.getMessagePool()[index].message != null && this.getMessagePool()[index].message instanceof  Function){
												this.getMessagePool()[index].message.call(this,element,flag);
											}
										}
									}
									if(allFlag){
										allFlag = flag;
									}
								}
							}
						}
					}
				}
			}
		}
		
		if(allFlag && element.getAttribute("_tmpUUIDKey") != null &&  element.getAttribute("_tmpUUIDKey") != ""){
			var tmpUUIDKey = element.getAttribute("_tmpUUIDKey");
			var oldObj = this.get(tmpUUIDKey);
			this.copyAttrs(oldObj,element.style);
		}

		return allFlag;
	};
}


ValidateFactory.prototype.validatePool = new Array();

ValidateFactory.prototype.messagePool = new Array();


//判断是否为空
ValidateFactory.prototype.isNotNull = function(value){
	var flag = false;
	if(value != undefined && value != null && this.trim(value) != ""){
		flag = true;
	}
	return flag;
};
//去掉左右两边的空格
ValidateFactory.prototype.trim = function(str){
	return str.replace(/(^\s*)|(\s*$)/g,"");
};
//转成Array对象
ValidateFactory.prototype.toArray = function(obj){
	var array = new Array();
	for(var i = 0; i < obj.length; i++){
		array.push(obj[i]);
	}
	return array;
};

//克隆对象生成新的对象(克隆一层)
ValidateFactory.prototype.clone = function(obj){
	var newObj = new Object(); 
	for(var o in obj){
		newObj[o] = obj[o];
	}
	return newObj;
};

//将targetObj不一样的属性值复制到sourceObj对应的属性中
ValidateFactory.prototype.copyAttrs = function(sourceObj, targetObj){
	for(var o in sourceObj){
		if(targetObj[o] != sourceObj[o]){
			targetObj[o] = sourceObj[o];
		}
	}
};


//批量添加验证
//validateName	: 验证标示
//vArr			: [{"nodeName":"元素名称","type":"元素的type值"},{"nodeName":"元素名称","type":"元素的type值"}]
//fun			: 具体的验证内容方法[第一个参数被验证的元素]
//message		: 验证后产生效果的方法[第一个参数被校验的元素,第二个参数校验后返回的结果(成功:true,失败:false)],不是必须的
ValidateFactory.prototype.addValidates = function(validateName, vArr, fun, message){
	if(fun instanceof  Function){
		var vlength = vArr.length;
		for(var i = 0; i < vlength; i++){
			vObj = vArr[i];
			var nodeName = vObj.nodeName.toUpperCase();
			var type = vObj.type.toUpperCase();
			this.addValidate(validateName.toUpperCase(), nodeName, type, fun, message);
		}
	}
};
//单个添加验证
//validateName	: 验证标示
//nodeName		: 元素名称 [如: input]
//type			: 元素类型 [如: text]
//fun			: 具体的验证内容方法[第一个参数被验证的元素]
//message		: 验证后产生效果的方法[第一个参数被校验的元素,第二个参数校验后返回的结果(成功:true,失败:false)],不是必须的
ValidateFactory.prototype.addValidate = function(validateName, nodeName, type, fun, message){
	if(fun instanceof  Function){
		var index = this.getNodeNameAndTypeIndex(nodeName, type);
		if(index == -1){
			var validater = {"nodeName":nodeName, "type":type, "validaterArr":[{"validateName":validateName.toUpperCase(), "fun":fun, "message":message}]};
			this.getValidatePool().push(validater);
		}else{
			var tmpValidaterArr = this.getValidatePool()[index].validaterArr;
			this.addValidater(tmpValidaterArr, fun, validateName, message);
		}
	}
};

ValidateFactory.prototype.addValidater = function(tmpValidaterArr, fun, validateName, message){
	var index = this.getValidaterIndex(tmpValidaterArr, validateName);
	if(index == -1){
		tmpValidaterArr.push({"validateName":validateName.toUpperCase(), "fun":fun, "message":message});
	}else{
		tmpValidaterArr[index].fun = fun;
		tmpValidaterArr[index].message = message;
	}
};

ValidateFactory.prototype.getNodeNameAndTypeIndex = function(nodeName, type){
	var index = -1;
	var vLength = this.getValidatePool().length;
	for(var i = 0; i < vLength; i++){
		var v = this.getValidatePool()[i];
		if(v.nodeName == nodeName && v.type == type){
			index = i;		
			break;
		}
	}
	return index;
};

ValidateFactory.prototype.getValidaterIndex = function(tmpValidaterArr, validateName){
	var index = -1;
	var tvArrLength = tmpValidaterArr.length;
	for(var i = 0; i < tvArrLength; i++){
		var tvr = tmpValidaterArr[i];
		if(tvr.validateName == validateName){
			index = i;
			break;
		}
	}
	return index;
};
//批量添加验证提示
//mArr    : [{"nodeName":"元素名称","type":"元素的type值"},{"nodeName":"元素名称","type":"元素的type值"}]
//message : 验证后产生效果的方法第一个参数被校验的元素,第二个参数校验后返回的结果(成功:true,失败:false)
ValidateFactory.prototype.addMessages = function(mArr, message){
	if(message instanceof  Function){
		var mLength = mArr.length;
		for(var i = 0; i < mLength; i++){
			var nodeName = mArr[i].nodeName.toUpperCase();
			var type = mArr[i].type.toUpperCase();
			this.addMessage(nodeName, type, message);
		}
	}
};
//单个添加验证提示
//nodeName	: 元素名称 [如: input]
//type		: 元素类型 [如: text]
//message	: 验证后产生效果的方法第一个参数被校验的元素,第二个参数校验后返回的结果(成功:true,失败:false)
ValidateFactory.prototype.addMessage = function(nodeName, type, message){
	if(message instanceof  Function){
		var index = this.getMessageIndex(nodeName, type);
		if(index == -1){
			this.getMessagePool().push({"nodeName":nodeName, "type":type, "message":message});
		}else{
			var m = this.getMessagePool()[index];
			m.message = message;
		}
	}
};

ValidateFactory.prototype.getMessageIndex = function(nodeName, type){
	var index = -1;
	var mLength = this.getMessagePool().length;
	for(var i = 0; i < mLength; i++){
		var m = this.getMessagePool()[i];
		if(m.nodeName == nodeName && m.type == type){
			index = i;
			break;
		}
	}
	return index;
};

ValidateFactory.prototype.getValidatePool = function(){
	return ValidateFactory.prototype.validatePool;
};

ValidateFactory.prototype.getMessagePool = function(){
	return ValidateFactory.prototype.messagePool;
};

ValidateFactory.prototype.isValidate = true;

ValidateFactory.prototype.setIsValidate = function(value){
	ValidateFactory.prototype.isValidate = value;
};



//拦截form提交事件,绑定验证
ValidateFactory.prototype.validateSubmit = function(){
	var formArr = document.getElementsByTagName("FORM");
	if(formArr != null){
		for(var i = 0; i < formArr.length; i++){
			var formObj = formArr[i];
			var validation = formObj.getAttribute("validation");
			var idVal = formObj.getAttribute("id");
			var flag = false;
			if(ValidateFactory.prototype.isNotNull(validation)){
				validation = ValidateFactory.prototype.trim(validation);
				if(validation.toUpperCase() == "TRUE"){
					flag = true;
				}
			}else if(ValidateFactory.prototype.isNotNull(idVal)){
				idVal = ValidateFactory.prototype.trim(idVal);
				if(idVal.toUpperCase().indexOf("_V_") == 0){
					flag = true;
				}
			}
			if(flag){
				ValidateFactory.prototype.validateBindSubmit(formObj);
			}
		}
	}
};

ValidateFactory.prototype.validateBindSubmit = function (formObj){
	var v = new ValidateFactory();
	v.setFormObj(formObj);
	ValidateFactory.prototype.bind(formObj,"submit",
			function(e){
				var re = v.validate();
				if(!re){
					e.preventDefault ? e.preventDefault() : e.returnValue = false;
				}
				return re;
			}
		,false);
	formObj.oldSubmit = formObj.submit;
	formObj.submit = function (){
		if(v.validate()){
			this.oldSubmit();
		}else{
			return false;
		}
	};
};

ValidateFactory.prototype.bindFormId = function (formId){
	var formObj = document.getElementById(formId);
	if(formObj != undefined && formObj != null && formObj.nodeName.toUpperCase() == "FORM"){
		ValidateFactory.prototype.validateBindSubmit(formObj);
	}
};

ValidateFactory.prototype.bind = function (obj, eveName, funName, flag){
	if(navigator.appName.indexOf("Explorer") > -1){ 
		obj.attachEvent("on"+eveName, funName);
	}else{
		obj.addEventListener(eveName, funName, flag);
	}
};

ValidateFactory.prototype.bind(window,"load",ValidateFactory.prototype.validateSubmit,false);

ValidateFactory.prototype.lastPrompt = function (elementList){
		
		$(".formError").remove();
		
		for(var i = 0; i < elementList.length; i++){
			
			var element = elementList[i];
			var $element = $(element);
			var indexStr = $element.attr("v_index");
			
			var indexArr = indexStr.split(",");
			
			var message = "";
			
			for(var j = 0; j < indexArr.length; j++){
				var index = indexArr[j];
				if(message == ""){
					message = $element.attr("m"+index);
				}else{
					message += "<br/>"+$element.attr("m"+index);
				}
			}
			$element.prompt(message);
			
		}
	
};

ValidateFactory.prototype.addValidates(
	"NotEmpty",
	[{"nodeName":"input","type":"text"},{"nodeName":"textarea","type":"textarea"},{"nodeName":"input","type":"password"}],
	function(element){
		var val = element.value;
		val = ValidateFactory.prototype.trim(val);
		var flag = true;
		if(val == ""){
			flag = false;
		}
		return flag;
	}
);

ValidateFactory.prototype.addValidates(
	"NotZero",
	[{"nodeName":"input","type":"text"},{"nodeName":"textarea","type":"textarea"}],
	function(element){
		var val = element.value;
		val = ValidateFactory.prototype.trim(val);
		var flag = true;
		if(val == "0"){
			flag = false;
		}
		return flag;
	}
);

ValidateFactory.prototype.addValidates(
	"NotChecked",
       [{"nodeName":"input","type":"radio"}],
       function(element){
       
		var name = $(element).attr("name");
		return $("input[name='"+name+"']").is(":checked");
		
       }
);

ValidateFactory.prototype.addValidates(
	"NotSelected",
	[{"nodeName":"select","type":"select-one"}],
    function(element){
       
		var val = $(element).val();
		
	   	return (val != null && val != undefined && val != "");
		
    }
);

ValidateFactory.prototype.addValidates(
	"PositiveInt",
       [{"nodeName":"input","type":"text"}],
       function(element){
		reg=/^[0-9]*[1-9][0-9]*$/;  
		return reg.test($(element).val());
		
       }
);
